<template>
  <base-load-page :loadState="false" class="base-load-page">
    <template #head>
      <div style="padding-bottom: 15px" class="head-box">
        <div class="left-box">
          <base-input-search
            v-model="filterData.appName"
            clearable
            placeholder="请输入平台名称"
            style="width: 100%"
            :prefixIcon="IconSearch"
            @submit="getAxiosData(true)"
          ></base-input-search>
          <!--
            <bubble-select-local
            v-model="filterData.type"
            labelKey="label"
            valueKey="value"
            :filterable="false"
            style="width: 100%"
            :list="[
            { label: '抖音', value: 'mp-toutiao' },
            { label: '快手', value: 'mp-kuaishou' },
            { label: '微信', value: 'mp-weixin' },
            ]"
            placeholder="请选择平台"
            @change="getAxiosData(true)"
            ></bubble-select-local> 
          -->
        </div>
        <div class="right-box">
          <el-button type="success" :icon="IconPlus" @click="handleAddMiniProgram()">新增小程序</el-button>
        </div>
      </div>
    </template>
    <el-table ref="elTableRef" v-loading="baseData.loading" border height="100%" :data="tableData" showOverflowTooltip stripe style="width: 100%">
      <template #empty>
        <base-empty :description="description" :pageLoadButton="[{ text: '重新加载', callBack: () => getAxiosData() }]"></base-empty>
      </template>
      <el-table-column
        fixed
        type="index"
        :index="(index: number) => (filterData.current - 1) * filterData.size + index + 1"
        label="编号"
        sortable
        width="60"
        align="center"
        headerAlign="center"
      ></el-table-column>
      <el-table-column fixed label="平台名称" prop="appName" minWidth="120"></el-table-column>
      <el-table-column label="平台类型" prop="type" minWidth="80">
        <template #default="{ row }">
          <el-tag v-if="row.type === 'mp-kuaishou'" key="el-tag-1" size="small">快手</el-tag>
          <el-tag v-else-if="row.type === 'mp-toutiao'" key="el-tag-2" type="warning" size="small">抖音</el-tag>
          <el-tag v-else-if="row.type === 'mp-weixin'" key="el-tag-3" type="success" size="small">微信</el-tag>
          <el-tag v-else key="el-tag-4" type="danger" size="small">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="平台编码" prop="appCode" minWidth="140"></el-table-column>
      <el-table-column label="app_id" prop="appId" minWidth="200"></el-table-column>
      <el-table-column label="是否是上传短剧主号" prop="mainFlag" minWidth="160">
        <template #default="{ row }">
          <el-tag v-if="row.mainFlag === 1" key="el-tag-1" type="success" size="small">是</el-tag>
          <el-tag v-else key="el-tag-2" type="danger" size="small">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="微信原始id" prop="appOriginalId" minWidth="180"></el-table-column>
      <el-table-column label="备注" prop="remark" minWidth="140"></el-table-column>
      <el-table-column fixed="right" label="操作" width="140" align="center" headerAlign="center">
        <template #default="{ row }">
          <div style="display: flex; justify-content: space-around; overflow: hidden">
            <el-button link type="danger" size="small" style="margin-left: 0" :icon="IconDelete" @click="handleDel(row)">删除</el-button>
            <el-button link type="primary" size="small" :icon="IconEdit" @click="openEdit(row)">编辑</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <template #foot>
      <div class="pagination-box">
        <el-pagination
          v-model:currentPage="filterData.current"
          v-model:pageSize="filterData.size"
          :pageSizes="[20, 30, 40, 50]"
          :disabled="baseData.loading"
          :pagerCount="13"
          layout="total, prev, pager, next, sizes, jumper"
          :total="baseData.total"
          @change="getAxiosData(true)"
        ></el-pagination>
      </div>
    </template>
    <add-mini-program ref="addMiniProgramRef"></add-mini-program>
  </base-load-page>
</template>

<script setup lang="ts">
  import IconEdit from '~icons/ep/edit';
  import IconSearch from '~icons/ep/search';
  import IconPlus from '~icons/ep/plus';
  import IconDelete from '~icons/ep/delete';
  import { type MiniProgramListItem, type MiniProgramListRequest, delMiniProgram, getMiniProgramList } from '@/api/app-management';
  import AddMiniProgram from './components/add-mini-program.vue';

  onMounted(() => {
    getAxiosData();
  });

  const addMiniProgramRef = ref<InstanceType<typeof AddMiniProgram>>();
  const elTableRef: $RefComp<'ElTable'> = ref(null);
  const baseData = shallowReactive({
    type: 'add' as 'add' | 'edit',
    /** 加载状态 */
    loading: true,
    /** 总数量 */
    total: 0,
  });
  const filterData: Ref<MiniProgramListRequest> = ref<MiniProgramListRequest>({
    appName: '',
    type: '',
    current: 1,
    size: 40,
  });

  const tableData: Ref<MiniProgramListItem[]> = ref([]);
  const description = ref('');
  /**
   * 设置表格数据
   */
  async function getAxiosData(showLoading = true) {
    try {
      await nextTick();
      baseData.loading = Boolean(showLoading);
      const res = await getMiniProgramList(filterData.value);
      tableData.value = res.data.records;
      description.value = '暂无数据';
      filterData.value.current = res.data.current;
      filterData.value.size = res.data.size;
      baseData.total = res.data.total;
      baseData.loading = false;
    } catch (error) {
      baseData.loading = false;
      tableData.value = [];
      description.value = error.msg || '加载失败，-BD001';
    }
  }

  /** 编辑 */
  function openEdit(row: MiniProgramListItem) {
    addMiniProgramRef.value?.show({
      action: 'edit',
      record: row,
      editCallBack: () => {
        getAxiosData(false);
      },
    });
  }

  /**
   * 删除
   */
  function handleDel(row: MiniProgramListItem) {
    ElMessageBox.confirm('删除后不可恢复，您确定删除吗？', '温馨提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      draggable: true,
      beforeClose: async (action, instance, done) => {
        if (action === 'confirm') {
          instance.confirmButtonLoading = true;

          try {
            await delMiniProgram(row.socialId!);
            await getAxiosData(false);
            ElToast.success('删除成功');
          } catch (error) {
            ElToast.error(error.msg || '删除失败，-BD004');
          }
        }
        done();
      },
    });
  }

  /**
   * 新增小程序
   */
  function handleAddMiniProgram() {
    addMiniProgramRef.value?.show({
      action: 'add',
      addCallBack() {
        getAxiosData();
      },
    });
  }
</script>

<style lang="scss" scoped>
  .pagination-box {
    @include flex(row, nowrap, center, center, center);
    padding-top: 15px;
  }

  .head-box {
    @include flex(row, nowrap, flex-start, center, center);

    .left-box {
      display: grid;
      flex: 1;
      grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
      grid-gap: 15px;
      margin-right: 15px;
    }
  }
</style>
